package com.HPU.manager;


import com.HPU.entity.User;

import java.io.*;
import java.util.Scanner;

public class Manager {
    private Scanner sc = new Scanner(System.in);
    private User[] users = new User[50]; // 创建users数组 存取50个对象
    private final String FILE_PATH = "F://tel.txt";

    private void add() {
        User user = new User(); // 创建user对象
        System.out.print("姓名:");
        user.setName(sc.next());
        System.out.print("电话号码:");
        user.setPhone(sc.next());
        for (int i = 0; i < users.length; i++) {
            if (users[i] == null) {
                users[i] = user;
                break;
            }
        }
        System.out.println("新增成功");
    }

    private void modify() {
        System.out.print("姓名:");
        String name = sc.next();
        System.out.print("请输入新的电话号码:");
        String phone = sc.next();
        for (int i = 0; i < users.length; i++) {
            if (users[i] != null && users[i].getName().equals(name)) {
                users[i].setPhone(phone);
            }
        }
        System.out.println("修改成功");
    }

    private void check() {
        System.out.println("姓名\t电话");
        System.out.println("=================");
        for (User user : users) {
            if (user != null)
                System.out.println(user.getName() + "\t\t" + user.getPhone());
        }
    }

    private void remove() {
        System.out.print("姓名:");
        String name = sc.next();
        for (int i = 0; i < users.length; i++) {
            if (users[i] != null && users[i].getName().equals(name)) {
                users[i] = null;
            }
        }
        System.out.println("删除成功");
    }

    private void saveTel() {
        try {
            FileOutputStream out = new FileOutputStream(FILE_PATH);
            PrintWriter pw = new PrintWriter(out, true);
            for (User user : users) {
                if (user != null) {
                    pw.println(user.getName() + "\t\t" + user.getPhone());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void readTel() {
        File f = new File(FILE_PATH);
        if (!f.exists()) {
            return;
        } else {
            try {
                FileReader reader = new FileReader(f);
                BufferedReader br = new BufferedReader(reader);
                String str = br.readLine();
                System.out.println(str);
                while (str != null) {
                    String[] us = str.split("\t\t");
                    if (us.length == 2) {
                        User user = new User();
                        user.setName(us[0]);
                        user.setPhone(us[1]);
                        for (int i = 0; i < users.length; i++) {
                            if (users[i] == null) {
                                users[i] = user;
                                break;
                            }
                        }
                    }
                    str = br.readLine();
                    System.out.println(str);
//                    String str1 = br.readLine();
                }
                br.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void start() {
        System.out.println("开始通讯录");
        System.out.println();
        while (true) {
            System.out.println("通讯录");
            System.out.println("===========================");
            System.out.println("1.新增");
            System.out.println("2.修改");
            System.out.println("3.查看");
            System.out.println("4.删除");
            System.out.println("5.读取");
            System.out.println("6.退出");
            System.out.println("===========================");
            System.out.print("请输入功能编号：");
            String str = sc.next();
            if (str.equals("1")) {
                add();
                saveTel();
            } else if (str.equals("2")) {
                modify();
                saveTel();
            } else if (str.equals("3")) {
                check();
            } else if (str.equals("4")) {
                remove();
                saveTel();
            } else if (str.equals("5")) {
                readTel();
            } else if (str.equals("6")) {
                System.out.println("退出成功");
                break;
            } else {
                System.out.println("请重新输入");
                break;
            }
        }
    }
}
